var batterieTemplate = '<li class="exercice buttonMenu"></li>';

/**
 * Go to exercice choice
 */
function displayExercices() {
	hideContainers();
	$('.navigationArea .exerciceChoiceNavButton').addClass('hidden');
	showContainer('exerciceChoiceContainer');
}

/**
 * Going back from Exercices
 */
function backFromExercices() {
	if (hasOnlyOneExercice) {
		displayMainMenu();
	}else{
		displayBatteries();
	}
}

/**
 * Fill exercice
 * @param idx
 * @param batterie
 */
function fillExercice(idx, batterie) {
	var shortKey = $('.menu.exercices li').size() + 1;
	var batterieDom = $(batterieTemplate);
	batterieDom.attr('id', 'batterie_' + idx);
	batterieDom.attr('batterie', idx);
	batterieDom.addClass('shortKey' + shortKey);
	batterieDom.append($('<div class="shortKeyIcon"></div>')).append($('<a/>').html(batterie.nom).attr('href', '#').click(
		function(){
			selectExercice(batterie);
			return false;
		}
	));
	var currentRow = parseInt((shortKey - 1) / 3);
	if ($(".menu.exercices div.rowContainer")[currentRow] === undefined) {
		$(".menu.exercices").append($("<div/>").addClass("rowContainer"));
	}
	$($(".menu.exercices div.rowContainer")[currentRow]).append(batterieDom);
}

/**
 * Select exercice
 * @param idx
 */
function selectExercice(batterie) {
	selectedBatterie = batterie;
	$('.batterieDescription').html(batterie.description);
	// fill series
	emptySeries();
	if ((batterie.exercices == null) || (objectLength(batterie.exercices) == 0)) {
		displayPopup('Cette batterie est vide.');
	}else{
		$.each(batterie.exercices.series, fillSerie);

		// manage navigation
		if ($('body').hasClass('exerciceChoiceContainerDisplayed')) {
			$('.navigationArea .exerciceChoiceNavButton').html(batterie.nom);
			$('.navigationArea .exerciceChoiceNavButton').removeClass('hidden');
		}

		// check if there are more than a single serie
		if ($(".menu.series li").length == 1) {
			// display words of the only existing serie
			hasOnlyOneSerie = true;
			$(".menu.series li").find("a").trigger("click");
		} else {
			// display the right frame
			hasOnlyOneSerie = false;
			
			displaySeries();
		}
	}
}
